package cn.moremind.leetcode;

/**
 * @author <a href="mailto:hefe@bsoft.com.cn">hefengen</a>
 * @(#) Created on
 * 版权：版权所有 bsoft 保留所有权力。
 * @description
 */

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

class DemoApplicationTests {
    //读取600次(600M)
    public static final long defaultFileSize = 400;
    //一次读取1M
    public static final long defaultByteSize = 1024 * 1024;
    //分割后每个文件的大小
    public static final long defaultReadSize = defaultFileSize * defaultByteSize;

    /**
     * 文件分割
     */
    public static void main(String[] args) {
        //要被分割的文件
        File file = new File("D://tmp//test.txt");
        //分割后文件保存的路径及前缀
        String name = "D:\\tmp\\idea_";
        long length = file.length();
        long size = length / defaultReadSize;
        if (length % defaultReadSize > 0) {
            size = size + 1;
        }

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 10,
                1, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(11));

        for (long i = 0; i < 10; i++) {
            final String fileName = name + i + ".tmp";
            final long index = i;
            threadPool.execute(new Thread(() -> {
                try {
                    new cn.moremind.leetcode.FileDivisionTest().divisionFile(fileName, index, file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }));
        }
    }
}


